package sy.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.UserDaoI;
import sy.model.Tuser;
import sy.pageModel.DataGrid;
import sy.pageModel.User;
import sy.service.UserServiceI;
import sy.util.base.MD5Util;
import sy.util.ref.MyBeanUtils;

@Service("userService")
public class UserServiceImpl implements UserServiceI {

	@Autowired 
	private UserDaoI userDaoI;
 
	@Override
	public Serializable save(User u) {
		Tuser tUser=new Tuser(); 
		BeanUtils.copyProperties(u, tUser);
		tUser.setId(UUID.randomUUID().toString());
		tUser.setCreatedatetime(new Date());
		tUser.setPwd(MD5Util.md5(u.getPwd()));
		return userDaoI.save(tUser);
	}
	@Override
	public User login(User u) {
		//Tuser tuser=userDaoI.get("from Tuser where name=? and pwd=?",new Object[]{u.getName(),MD5Util.md5(u.getPwd())});
		Map<String,Object> map=new HashMap<String, Object>();
	    map.put("name", u.getName());
	    map.put("pwd", MD5Util.md5(u.getPwd()));
		Tuser tuser=userDaoI.get("select t from Tuser t where name=:name and pwd=:pwd",map);
 		if(tuser!=null){
 			BeanUtils.copyProperties(tuser, u);
 			return u;
 		}
 		return null;
	}
	@Override
	public  DataGrid dataGrid(User u) {
		DataGrid dg=new DataGrid();
		Map<String,Object> params=MyBeanUtils.getPropertiesMap(u);//模型map
        Map<String, Object>pageMap=MyBeanUtils.getSuperPropertiesAndValueMap(u);//分页模型map
        String hql=" from Tuser "; 
        String  where =addWhere(params);
        String order =addOrder(pageMap);
        if(where!=null){
        	hql=hql+where;
        }
 		dg.setTotal(userDaoI.count("select count(*) "+hql, params).intValue());
        if(order!=null){
        	hql=hql+order;
        }
 		List<Tuser> l=userDaoI.find(hql, params, u.getPage(), u.getRows());
		List<User>ul=new ArrayList<User>();
		if(l!=null&&l.size()>0){
			for(int i=0;i<l.size();i++){
				User user = new User();
 				BeanUtils.copyProperties(l.get(i), user);
 				ul.add(user);
			}
		}
		dg.setRows(ul);
		 
		return dg;
	}
    public String addWhere(Map<String,Object> params){
    	String where="";
    	if(params!=null&&!params.isEmpty()){
    		for(String key:params.keySet()){
    			Object value=params.get(key);
    			if(value!=null&&!"".equals(value.toString().trim())){
    				if(value instanceof String){
    				      where=where+" and @col like :@col ";
    				}else {
    					   where=where+" and @col =:@col ";
					}
    				where=where.replaceAll("@col", key);
    			}
    		}
    	}
    	if(where!=null&&!"".equals(where)){
    		  return "where 1=1 "+where;
    	}
    	return null;
    }
    @Override
	public int remove(String hql) {
		return userDaoI.executeHql(hql);
	}
    @Override
    public User edit(User u){
    	Tuser tuser=userDaoI.get(Tuser.class, u.getId());
     	u.setModifydatetime(new Date());
    	BeanUtils.copyProperties(u, tuser,new String[]{"pwd","id","createdatetime"});
    	return u;
    }
	public String addOrder(Map<String,Object> params){
    	 if(params!=null&&!params.isEmpty()){
    		 String sort=(String) params.get("sort");
    		 String order=(String) params.get("order");
    		 if(sort!=null){
    			 return "order by "+sort+" "+ order;
    		 }
    	 }    	
    	return null;
    }
    
}
